<?php
namespace Api\Controller;

use Think\Controller;
use Api\Model\LoginModel;

/**
 *抢单接口
 */
class GrabOrderController extends Controller
{
    /**
     * 抢单首页
     * @param  string $user_name
     * @param  string $key
     * @param  string $r_id
     * @return array
     * @author lyz
     */
    public function index(){
        echo 111;
    }
    /**
     * 开启订单模式
     *
     * @param  string $user_name
     * @param  string $key
     * @param  string $r_id
     * @access public
     * @return array
     * @author lyz
     */
    public function startOrderStatus()
    {
        //密钥,用户名称,是否开工
        #判断密钥是否和redis里面的相同
        #判断该用户是否是师傅
        #修改用户表听单的字段
        $return = array('errcode' => 1001, 'errmsg' => 'key不匹配');
        if (IS_POST) {
            $user_name = I('post.user_name');
            $key = I('post.key');
            var_dump($_POST);
            if (empty($key) || empty($user_name)) {

                $return['errcode'] = 2001;
                $return['errmsg'] = '请按照要求传参';
                $this->ajaxReturn($return, 'JSON');//key不相同
            }
            if (!check_sign($user_name, $key)) {
                $this->ajaxReturn($return, 'JSON');//key不相同
            }
            $userInfo =  M('user')->where(array('user_name'=>$user_name, 'role' => 2))->limit(1)->find();
            if (empty($userInfo)) {
                $return['errcode'] = 2002;
                $return['errmsg'] = '角色非师傅或者用户不存在';
                $this->ajaxReturn($return, 'JSON');//角色非师傅
            }
            if ($userInfo[0]['is_listen'] == 1) {
                $return['errcode'] = 2003;
                $return['errmsg'] = '师傅已经开启听单模式';
                $this->ajaxReturn($return, 'JSON');//角色非师傅
            }
            $flag = M('user')->where(array('user_name' => $user_name))->save(array('is_listen' => 1));
            if ($flag > 0) {
                $return['errcode'] = 200;
                $return['errmsg'] = '操作成功';
            }
        }

    }


}